package cn.maihe.elg.operation.web.invoice;

import cn.maihe.elg.operation.exception.ErrorCode;
import cn.maihe.elg.operation.exception.SystemRuntimeException;
import cn.maihe.elg.operation.model.enums.UserType;
import cn.maihe.elg.operation.repository.entity.InvoiceInfoDO;
import cn.maihe.elg.operation.repository.entity.InvoiceInfoVo;
import cn.maihe.elg.operation.security.model.UserContext;
import cn.maihe.elg.operation.service.invoice.InvoiceInfoService;
import cn.maihe.elg.operation.utils.UserContextUtil;
import cn.maihe.framework.starter.base.sql.PageBo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description 开票信息controller
 * @Author caixh
 * @Date 2021/4/14.
 */
@Api(value = "开票信息管理", tags = "开票信息管理")
@RestController
@RequestMapping("/api")
@Slf4j
public class InvoiceInfoController {
    @Resource
    private InvoiceInfoService invoiceInfoService;

    @ApiOperation(value = "分页查询开票信息", notes = "根据开票信息类型、机构编码、机构名称 分页查询")
    @RequestMapping(value = "/invoiceInfos", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "invoiceInfoType", value = "保函类型", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "invoiceInfoCategory", value = "保函品种", dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "Name", value = "开票信息名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "gmtCreateRange", value = "创建时间（startDate,endDate）", dataType = "String", paramType = "query")
    })
    public PageBo<InvoiceInfoDO> pageList(int pageNo, int pageSize, String gmtCreateRange, InvoiceInfoVo vo) {
        //登录机构过滤
        UserContext uc= UserContextUtil.getUserContext();
        if(UserType.GENERAL.getType() != uc.getUserType()){
            vo.setOrgId(uc.getOrgId());
        }
        return invoiceInfoService.selectPageBySql(pageNo, pageSize, gmtCreateRange, vo);
    }

    @ApiOperation(value = "获取开票信息", notes = "根据开票信息id获取开票信息")
    @ApiImplicitParam(name = "id", value = "开票信息id", dataType = "long", paramType = "path", required = true)
    @RequestMapping(value = "/invoiceInfo/{id}", method = RequestMethod.GET)
    public InvoiceInfoDO get(@PathVariable Long id) {
        InvoiceInfoDO curDO = invoiceInfoService.selectById(id);
        if (curDO == null) {
            throw new SystemRuntimeException(ErrorCode.NOTEXISTS, "开票信息id");
        }
        return curDO;
    }

}
